package demo.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
//import org.springframework.security.authentication.AnonymousAuthenticationToken;
//import org.springframework.security.authentication.AuthenticationServiceException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
//import org.springframework.security.authentication.AnonymousAuthenticationToken;
//import org.springframework.security.authentication.AuthenticationServiceException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
//import com.netflix.zuul.context.RequestContext;
//
//import io.jmnarloch.spring.cloud.ribbon.support.RibbonFilterContextHolder;

@Component
public class AccessFilter extends ZuulFilter {

	Logger logger = LoggerFactory.getLogger(AccessFilter.class);

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		logger.info("**************Pre AccessFilter ***********RequestURI:{}", RequestContext.getCurrentContext().getRequest().getRequestURI());
		
		//get the access token from security context
//		Object token = WebContextUtil.getAccessToken();
//		logger.info("**************Pre AccessFilter  token:{}", token);
//
//		//add the access token to Authorization header
//		RequestContext ctx = RequestContext.getCurrentContext();
//		ctx.addZuulRequestHeader("Authorization", "Bearer " + token);

		// 灰度示例//MetadataAwarePredicate
//		RibbonFilterContextHolder.clearCurrentContext();		
//		String region = ctx.getRequest().getParameter("region");
//		if ("China".equals(region)) {
//			//China region uses the new service
//			RibbonFilterContextHolder.getCurrentContext().add("gated-launch", "true");
//		} else {
//			RibbonFilterContextHolder.getCurrentContext().add("gated-launch", "false");
//		}
		
		return null;
	}

	@Override
	public String filterType() {
		return FilterConstants.PRE_TYPE;
	}

	@Override
	public int filterOrder() {
		return 0;
	}
}

//class WebContextUtil {
//	/**
//	 * 获取当前上下文授权信息
//	 * 
//	 * @return
//	 */
//	public static Authentication getAuthentication() {
//		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//		if (!(authentication instanceof AnonymousAuthenticationToken)) {
//			return authentication;
//		}
//		throw new AuthenticationServiceException("authentication not found");
//	}
//
//	/**
//	 * 获取当前上下文token的信息
//	 * 
//	 * @return
//	 */
//	public static OAuth2AuthenticationDetails getDetails() {
//		Authentication authentication = getAuthentication();
//		if (authentication == null) {
//			return null;
//		}
//		OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
//		return details;
//	}
//
//	/**
//	 * 获取当前登入用户账号
//	 * 
//	 * @return
//	 */
//	public static String getUsername() {
//		return getAuthentication().getName();
//	}
//
//	/**
//	 * 获取当前登入用户的访问accessToken
//	 * 
//	 * @return
//	 */
//	public static String getAccessToken() {
//		if (getDetails() == null) {
//			return null;
//		}
//		return getDetails().getTokenValue();
//	}
//}
